package com.shadow.demo.data.mapping.reader;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;

import java.io.IOException;

@Slf4j
public abstract class BaseReader<T> {

    /* 更新时间 */
    @Getter
    protected long lastModified;
    /* 缓存 */
    protected T cachedJsonContent;
    /* 文件 */
    protected Resource cachedJsonResource;

    /**
     * 获取缓存内容.
     * */
    public T getJsonContent() {
        return cachedJsonContent;
    }

    /**
     * 判断文件是否更新.
     * */
    public boolean isFileUpdated() {
        try {
            return cachedJsonResource == null || cachedJsonResource.lastModified() > lastModified;
        } catch (IOException e) {
            log.error("get file lastModified error", e);
            return false;
        }
    }
}
